package com.wangsd.system.service;

import com.gitee.fastmybatis.core.query.Query;
import com.gitee.fastmybatis.core.query.Sort;
import com.wangsd.common.utils.MyDecimalUtils;
import com.wangsd.member.entity.GoldRecord;
import com.wangsd.member.entity.Member;
import com.wangsd.member.entity.MemberCoupon;
import com.wangsd.member.service.GoldRecordService;
import com.wangsd.member.service.MemberCouponService;
import com.wangsd.member.service.MemberService;
import com.wangsd.system.entity.Trade;
import com.wangsd.system.mapper.TradeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 订单Service业务层处理
 *
 * @author wsd
 * @date 2021-06-25
 */
@Service
public class TradeService {
    @Autowired
    private TradeMapper tradeMapper;
    @Autowired
    private MemberCouponService memberCouponService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private GoldRecordService goldRecordService;
    /**
     * 查询订单
     *
     * @param id 订单ID
     * @return 订单
     */
    public Trade selectTradeById(Long id) {
        return tradeMapper.getById(id);
    }

    /**
     * 查询订单列表
     *
     * @param trade 订单
     * @return 订单
     */
    public List<Trade> selectTradeList(Trade trade) {
        Query query = new Query();
        query.orderby("id", Sort.DESC);
        List<Trade> list = tradeMapper.list(query);
        return list;
    }

    /**
     * 新增订单
     *
     * @param trade 订单
     * @return 结果
     */
    public int insertTrade(Trade trade) {
        return tradeMapper.saveIgnoreNull(trade);
    }

    /**
     * 修改订单
     *
     * @param trade 订单
     * @return 结果
     */
    public int updateTrade(Trade trade) {
        return tradeMapper.updateIgnoreNull(trade);
    }

    /**
     * 删除订单信息
     *
     * @param id 订单ID
     * @return 结果
     */
    public int deleteTradeById(Long id) {
        return tradeMapper.deleteById(id);
    }

    /**
     * 查询订单
     *
     * @param outTradeNo 订单号
     * @return 订单
     */
    public Trade selectTradeByTradeNo(String outTradeNo) {
        return tradeMapper.getByColumn("out_trade_no", outTradeNo);
    }

    /**
     * 修改订单状态,并扣除金币和优惠券
     * @param trade
     */
    public void updateTradeStatus(Trade trade) {
        trade.setStatus(1);
        updateTrade(trade);
        //如果用户使用了优惠券
        if (trade.getMemberCouponId() > 0) {
            MemberCoupon memberCoupon = new MemberCoupon();
            memberCoupon.setId(trade.getMemberCouponId());
            memberCoupon.setStatus(2);
            memberCouponService.updateMemberCoupon(memberCoupon);
        }
        //如果用户使用了金币抵扣
        if (trade.getUseGoldNum() > 0) {
            Member member = memberService.selectMemberById(trade.getMemberId());
            Double updateAfter = MyDecimalUtils.sub(member.getGoldNum(), trade.getUseGoldNum());
            GoldRecord goldRecord = new GoldRecord();
            goldRecord.setMemberId(member.getId());
            goldRecord.setType(2);
            goldRecord.setUpdateBefore(member.getGoldNum());
            goldRecord.setUpdateNum(trade.getUseGoldNum());
            goldRecord.setUpdateAfter(updateAfter);
            goldRecordService.insertGoldRecord(goldRecord);
            member.setGoldNum(updateAfter);
            memberService.updateMember(member);
        }
    }
}
